#include "Listener.h"
#include "Util.h"
#include "App.h"
#include "Sprite.h"
#include "GameMessages.h"

#include <stack>
#include <iostream>
#include <algorithm>

typedef struct{
	bool	isStatic;
}mob;

std::map<int,std::vector<mob*>>	worldMap;
vec3							mapOrigin;
int								viewDepthTiles=255;
Model*							mapStaticMesh=null;

SpriteDef*						staticMapSpriteDef=null;
Sprite*							staticMapSprite=null;
int								frameCounter=0;

void	setTerrainMesh(std::vector<vec4>&	vertices,std::vector<int>&	indices);

void		buildStaticVisibleMapRegion(){
	ModelBuilder	gb;
	if(mapStaticMesh)
		delete mapStaticMesh;
	mapStaticMesh=null;
	int	renderDim=4;
	float worldScale=5.0f;
	std::vector<int>	physicsIndexBuffer;
	for(int ty=-renderDim;ty<renderDim;ty++){
		for(int tx=-renderDim;tx<renderDim;tx++){
			const char*	geomNames[]={"tile_flat","tile_flat2","tile_floor","tile_leftWall","tile_leftWall2","tile_leftDoor","tile_machine","tile_machine2"};
			vec3 to=vec3(tx*2.0f,ty*2.0f,0.0f)*worldScale;
			int	remap[4][6]={{0,1,2,1,1,1},{1,0,2,1,-1,1},{0,1,2,-1,-1,1},{1,0,2,-1, 1,1}};
			int rdir=rand()%4;
			gb.addModel(SpriteDef::getModel(geomNames[rand()%3]),to,worldScale,&remap[rdir][0],true);//(rand()&255)%4
			if(!(rand()&7))
				gb.addModel(SpriteDef::getModel(geomNames[(rand()%3)+3]),to,worldScale,&remap[rdir][0],true);//(rand()&255)%4
		}
	}
	if(gb.hasGeometry()){
		mapStaticMesh=gb.getModel("station13",gApp->getRenderer());
		if(gb.physVertices.size()&&gb.physIndices.size()){
			setTerrainMesh(gb.physVertices,gb.physIndices);
		}
		staticMapSpriteDef->models[0]=mapStaticMesh;
	}
}

class	MapEditorListener : public Listener
{
public:
	virtual bool	message(const char *msg=null,void*	params=null)
	{
		if(msg=="engineStep"){

		}else if(msg=="engineRenderSolid"){
		}else if(msg=="engineRender"){
			
		if (gApp->keys[resetKey]){
			//#include "..\Listener.h"
			Listener::sendMessage("resetCamera");
		}
		//Listener::sendMessage("resetCamera");
		//	if((++frameCounter%1000)==0){
		//		buildStaticVisibleMapRegion();
		//	}
		}else if(msg=="engineDrawSelected"){
		}else if(msg=="engineDoPicking"){
		}else if(msg=="engineBoot"){
			
			staticMapSpriteDef=new SpriteDef();
			staticMapSprite=new Sprite();
			staticMapSprite->definition=staticMapSpriteDef;
			//gApp->camTargetID=staticMapSprite->id;
			if(!loadSpriteDef("testTiles",SPRITE_SHADER,staticMapSpriteDef)) //txonly
				return false;
		//	delete staticMapSpriteDef->models[0];
			staticMapSpriteDef->models[0]=null;
			//buildStaticVisibleMapRegion();



		}else if(msg=="engineShutdown"){
		}
		return true;
	}
	MapEditorListener(){
		Listener::listen(this,"engineStep");
		Listener::listen(this,"engineBoot");
		Listener::listen(this,"engineRender");
		Listener::listen(this,"engineRenderSolid");
		Listener::listen(this,"engineShutdown");
		Listener::listen(this,"engineDoPicking");
		Listener::listen(this,"engineDrawSelected");
	}	
	virtual ~MapEditorListener(){}
};

MapEditorListener gMapLord;